package com.yanqu.road.entity.activity.yqhotel.data;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.entity.FieldTypeAnnotation;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class YqHotelStudentData {
    //学生编号
    private int no;

    private String nickName;

    //1普通魂学员2双生魂学员
    private int type;

    //出门时间
    private long startTime;

    //左武魂类型
    private int leftSoulType;
    //右武魂类型
    private int rightSoulType;

    //buildingId soul  (0表示刚出生时候的魂)
    private Map<Integer,Long> leftSoulMap;

    private Map<Integer,Long> rightSoulMap;

    private Map<Integer,Long> waitMap;

    //需求改成学习时间也会变了
    private Map<Integer,Long> studyTimeMap;

    private Map<Integer,Long> enterTimeMap;

    //在哪个建筑id挂壁了
    private int dieInBuilding = -1;

    private long dieTime = -1;

    private Map<Integer,Integer> quickBuilding = new ConcurrentHashMap<>();

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public long getStartTime() {
        return startTime;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }


    public Map<Integer, Long> getLeftSoulMap() {
        return leftSoulMap;
    }

    public void setLeftSoulMap(Map<Integer, Long> leftSoulMap) {
        this.leftSoulMap = leftSoulMap;
    }

    public Map<Integer, Long> getRightSoulMap() {
        return rightSoulMap;
    }

    public void setRightSoulMap(Map<Integer, Long> rightSoulMap) {
        this.rightSoulMap = rightSoulMap;
    }

    public Map<Integer, Long> getWaitMap() {
        return waitMap;
    }

    public void setWaitMap(Map<Integer, Long> waitMap) {
        this.waitMap = waitMap;
    }

    public boolean isSetWaitTime(int buildingId){
        if(!waitMap.containsKey(buildingId)){
            return false;
        }
        return waitMap.get(buildingId) != -1;
    }

    public long getWaitTime(int buildingId) {
        if(waitMap.containsKey(buildingId) && waitMap.get(buildingId) > 0){
            return waitMap.get(buildingId);
        }
        return 0;
    }

    public void setWaitTime(int buildingId,long waitTime) {
        this.waitMap.put(buildingId,waitTime);
    }

    public boolean isQuickStudy(){
        return this.studyTimeMap != null;
    }

    public Map<Integer, Long> getStudyTimeMap() {
        return studyTimeMap;
    }

    public void setStudyTimeMap(Map<Integer, Long> studyTimeMap) {
        this.studyTimeMap = studyTimeMap;
    }

    //改变学习时间，大于这个建筑的都是受影响
    public void setStudyTime(int buildingId, long studyTime) {
        long loseTime = studyTimeMap.get(buildingId) - studyTime;
        studyTimeMap.put(buildingId,studyTime);
        for(int bId : getEnterTimeMap().keySet()){
            if(bId > buildingId){
                getEnterTimeMap().put(bId,getEnterTimeMap().get(bId) - loseTime);
            }
        }
    }

    public Map<Integer, Long> getEnterTimeMap() {
        return enterTimeMap;
    }

    public void setEnterTimeMap(Map<Integer, Long> enterTimeMap) {
        this.enterTimeMap = enterTimeMap;
    }

    public int getDieInBuilding() {
        return dieInBuilding;
    }

    public void setDieInBuilding(int dieInBuilding) {
        this.dieInBuilding = dieInBuilding;
    }

    public long getDieTime() {
        return dieTime;
    }

    public void setDieTime(long dieTime) {
        this.dieTime = dieTime;
    }


    public int getLeftSoulType() {
        return leftSoulType;
    }

    public void setLeftSoulType(int leftSoulType) {
        this.leftSoulType = leftSoulType;
    }

    public int getRightSoulType() {
        return rightSoulType;
    }

    public void setRightSoulType(int rightSoulType) {
        this.rightSoulType = rightSoulType;
    }


    public Map<Integer, Integer> getQuickBuilding() {
        return quickBuilding;
    }

    public void addQuickBuilding(int buildingId) {
        this.quickBuilding.put(buildingId,buildingId);
    }

    //转化成要保存的数据
    public String toSaveString() {
        List<String> strings = new ArrayList<>();
        strings.add(String.valueOf(no));                    //0位
        strings.add(nickName == null ? "" : nickName);      //1位
        strings.add(String.valueOf(type));                  //2位
        strings.add(String.valueOf(startTime));             //3位
        strings.add(String.valueOf(leftSoulType));          //4位
        strings.add(String.valueOf(rightSoulType));         //5位
        strings.add(JSON.toJSONString(leftSoulMap));        //6位
        strings.add(JSON.toJSONString(rightSoulMap));       //7位
        strings.add(JSON.toJSONString(waitMap));            //8位
        strings.add(JSON.toJSONString(studyTimeMap));       //9位
        strings.add(JSON.toJSONString(enterTimeMap));       //10位
        strings.add(String.valueOf(dieInBuilding));         //11位
        strings.add(String.valueOf(dieTime));               //12位
        strings.add(JSON.toJSONString(quickBuilding));      //13位
        return JSON.toJSONString(strings);
    }

    //解析
    public YqHotelStudentData(String jsonStr) {
        List<String> strings = JSON.parseObject(jsonStr, new TypeReference<List<String>>() {});
        this.no = Integer.parseInt(strings.get(0));
        this.nickName = strings.get(1);
        this.type = Integer.parseInt(strings.get(2));
        this.startTime = Long.parseLong(strings.get(3));
        this.leftSoulType = Integer.parseInt(strings.get(4));
        this.rightSoulType = Integer.parseInt(strings.get(5));
        String param6 = strings.get(6);
        this.leftSoulMap = param6 == null ? new ConcurrentHashMap<>():JSON.parseObject(param6, new TypeReference<ConcurrentHashMap<Integer, Long>>() {});
        String param7 = strings.get(7);
        this.rightSoulMap = param7 == null ? new ConcurrentHashMap<>():JSON.parseObject(param7, new TypeReference<ConcurrentHashMap<Integer, Long>>() {});
        String param8 = strings.get(8);
        this.waitMap = param8 == null ? new ConcurrentHashMap<>():JSON.parseObject(param8, new TypeReference<ConcurrentHashMap<Integer, Long>>() {});
        String param9 = strings.get(9);
        this.studyTimeMap = param9 == null ? new ConcurrentHashMap<>():JSON.parseObject(param9, new TypeReference<ConcurrentHashMap<Integer, Long>>() {});
        String param10 = strings.get(10);
        this.enterTimeMap = param10 == null ? new ConcurrentHashMap<>():JSON.parseObject(param10, new TypeReference<ConcurrentHashMap<Integer, Long>>() {});

        this.dieInBuilding = Integer.parseInt(strings.get(11));
        this.dieTime = Long.parseLong(strings.get(12));
        String param13 = strings.get(13);
        this.quickBuilding = param13 == null ? new ConcurrentHashMap<>():JSON.parseObject(param13, new TypeReference<ConcurrentHashMap<Integer, Integer>>() {});
    }

    //新一个
    public YqHotelStudentData() {

    }


    public static void main(String[] args) {
        YqHotelStudentData yqHotelStudentData = new YqHotelStudentData();
        String saveString = yqHotelStudentData.toSaveString();
        System.out.println(saveString);
        YqHotelStudentData student = new YqHotelStudentData(saveString);
        System.out.println(student.toSaveString());

    }

}
